/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package intelligenttm.ITMCarto;

import intelligenttm.SourceFile.BaseMapCString;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import me.aviator.entity.HibernateUtil;
import me.aviator.entity.MBasemap;
import me.aviator.service.impl.BasemapService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * FXML Controller class
 *
 * @author 望舒凌霄
 */
public class S1SelectMapController implements Initializable {
    @FXML
    private ChoiceBox<String> CBFeatureType;
    @FXML
    private ChoiceBox<String> CBMapProjection;
    @FXML
    private ChoiceBox<String> CBScale;
    @FXML
    private ChoiceBox<String> CBLevel;
    @FXML
    private ChoiceBox<String> CBArea;
    @FXML
    private Button BtnCancel;
    @FXML
    private Button BtnLast;
    @FXML
    private Button BtnNext;

    @FXML
    private TableView<MBasemap> MapTableView;
    @FXML
    private TableColumn<MBasemap, String> TCMapName;
    @FXML
    private TableColumn<MBasemap, ImageView> TCMapPic;
    @FXML
    private TableColumn<MBasemap, String> TCMapFeature;
    @FXML
    private TableColumn<MBasemap, String> TCMapProjection;
    @FXML
    private TableColumn<MBasemap, String> TCMapScale;
    @FXML
    private TableColumn<MBasemap, String> TCMapLevel;
    @FXML
    private TableColumn<MBasemap, String> TCMapArea;
    
    private BasemapService basemapService;
    private Map<String,String> conditions;
    
    private final BaseMapCString StringBox=new BaseMapCString();
    private String FeatureTypeString="全部";
    private String ProjectionString="双标准纬线等\n面积圆锥投影";
    private String ScaleString="全部";
    private String LevelString="国家";
    private String AreaString="中国";
    
    private MBasemap BM=new MBasemap();
    private ObservableList<MBasemap> MBasemapData =  FXCollections.observableArrayList();
    
    private String MapID;
    private int WhichBtn=0;
    @FXML
    private AnchorPane RootPane;
    /**
     * Initializes the controller class.
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        //预添加要素分类
        SessionFactory sf=HibernateUtil.getSessionFactory();
        Session sess=sf.openSession();
        Transaction tx=sess.beginTransaction();
        
        basemapService=new BasemapService(sess);
        
        String[] S=StringBox.getFeatureType();
        ObservableList<String> FeatureT=FXCollections.observableArrayList();
        FeatureT.addAll(Arrays.asList(S));
        CBFeatureType.setItems(FeatureT);
        CBFeatureType.getSelectionModel().select(0);
        
        //预添加投影
        ObservableList<String> MapP=FXCollections.observableArrayList();
        S=StringBox.getMapProjection();
        MapP.addAll(Arrays.asList(S));
        CBMapProjection.setItems(MapP);
        CBMapProjection.getSelectionModel().select(0);
        //预添加比例尺
        ObservableList<String> ScaleS=FXCollections.observableArrayList();
        S=StringBox.getScale();
        ScaleS.addAll(Arrays.asList(S));
        CBScale.setItems(ScaleS);
        CBScale.getSelectionModel().select(0);
        
        //预添加分级
        ObservableList<String> LevelS=FXCollections.observableArrayList();
        S=StringBox.getLevel();
        LevelS.addAll(Arrays.asList(S));
        CBLevel.setItems(LevelS);
        CBLevel.getSelectionModel().select(0);
        
        //预添加区域
        ObservableList<String> AreaS=FXCollections.observableArrayList();
        S=StringBox.getArea();
        AreaS.addAll(Arrays.asList(S));
        CBArea.setItems(AreaS);
        CBArea.getSelectionModel().select(0); 
        conditions=new HashMap<>();
        if(!FeatureTypeString.equals("全部")){
            conditions.put("catagory", FeatureTypeString);
        }
        conditions.put("project",ProjectionString);
        if(!ScaleString.equals("全部")){
            conditions.put("scale", ScaleString);
        }
        
        conditions.put("administration", LevelString);
        conditions.put("scope", AreaString);
        
        CBFeatureType.getSelectionModel().selectedIndexProperty().addListener((ov,oldv,newv)->{ 
            FeatureTypeString=CBFeatureType.getItems().get(newv.intValue());
            if(FeatureTypeString.equals("全部")){
                conditions.remove("catagory");
            }
            else
            {
                conditions.put("catagory", FeatureTypeString);
            }
            LoadMBasemapDate();
        });
        
        CBMapProjection.getSelectionModel().selectedIndexProperty().addListener((ov,oldv,newv)->{ 
            ProjectionString=CBMapProjection.getItems().get(newv.intValue());
            if(ProjectionString.equals("全部")){
                conditions.remove("project");
            }
            else
            {
                conditions.put("project",ProjectionString);
            }
            LoadMBasemapDate();
        });
        
        CBScale.getSelectionModel().selectedIndexProperty().addListener((ov,oldv,newv)->{ 
            ScaleString=CBScale.getItems().get(newv.intValue());
            if(ScaleString.equals("全部")){
                conditions.remove("scale");
            }
            else
            {
                conditions.put("scale", ScaleString);
            }
            LoadMBasemapDate();
        });
        
        CBLevel.getSelectionModel().selectedIndexProperty().addListener((ov,oldv,newv)->{ 
            LevelString=CBLevel.getItems().get(newv.intValue());
            conditions.put("administration", LevelString);
            LoadMBasemapDate();
        });
        CBArea.getSelectionModel().selectedIndexProperty().addListener((ov,oldv,newv)->{ 
            AreaString=CBArea.getItems().get(newv.intValue());
            conditions.put("scope", AreaString);
            LoadMBasemapDate();
        });
        SetTableColumn();
        LoadMBasemapDate();
//        TCMapName.setStyle("vertical-align:middle");
//        MapTableView. 
    }    
    
    private void SetTableColumn(){
        TCMapName.setCellValueFactory(new PropertyValueFactory<>("name"));
        TCMapPic.setCellValueFactory(new PropertyValueFactory<>("image"));
        TCMapFeature.setCellValueFactory(new PropertyValueFactory<>("catagory"));
        TCMapProjection.setCellValueFactory(new PropertyValueFactory<>("project"));
        TCMapScale.setCellValueFactory(new PropertyValueFactory<>("scale"));
        TCMapLevel.setCellValueFactory(new PropertyValueFactory<>("administration"));
        TCMapArea.setCellValueFactory(new PropertyValueFactory<>("scope"));
    }
    
    public void setBasemapService(BasemapService basemapService){
        this.basemapService=basemapService;
    }
    
    private void LoadMBasemapDate(){
        MBasemapData.clear();
        String Path;
        List<MBasemap> basemaps=basemapService.findSvgMap(conditions);
        if(basemaps!=null){
            MBasemapData.addAll(basemaps);
        }
        
        MapTableView.setItems(MBasemapData);
//        MapTableView.getSelectionModel().select(0);
    }
    
    public void setMBasemap(MBasemap tBM){
        BM=tBM;
        FeatureTypeString=tBM.getCatagory();
        CBFeatureType.getSelectionModel().select(FeatureTypeString);
        ProjectionString=tBM.getProject();
        CBMapProjection.getSelectionModel().select(ProjectionString);
        ScaleString=tBM.getScale();
        CBScale.getSelectionModel().select(ScaleString);
        LevelString=tBM.getAdministration();
        CBLevel.getSelectionModel().select(LevelString);
        AreaString=tBM.getScope();
        CBArea.getSelectionModel().select(AreaString);
        LoadMBasemapDate();
        for (int i=0;i<MBasemapData.size();i++){
            String MID=String.format("%d", MBasemapData.get(i).getId());
            if (Integer.parseInt(MID)==tBM.getId()){
                MapTableView.getSelectionModel().select(i);
            }
        }
    }
    
    public String getMapID() {
        return MapID;
    }

    public MBasemap getBM() {
        return BM;
    }

    public int getWhichBtn() {
        return WhichBtn;
    }
    
    @FXML
    private void HandleBtnCancel(ActionEvent event) {
        WhichBtn=0;
        RootPane.getScene().getWindow().hide();
    }
    
    @FXML
    private void HandleBtnNext(ActionEvent event) {
        int Index=MapTableView.getSelectionModel().getFocusedIndex();
        BM=MBasemapData.get(Index);
        MapID=String.format("%d", BM.getId());
        System.out.println(MapID);
        WhichBtn=1;
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnLast(ActionEvent event) {
        WhichBtn=-1;
        RootPane.getScene().getWindow().hide();
    }
    
}
